<?php
/**
 * 线下案件
 */
class Cli_LineunderController extends Ctrl_Cli{
    public function indexAction(){
		while(true){
            $tTime = time();
            $tRunlogdir = APPLICATION_PATH . '/shell/runlog/cli_lineunder.run';
            Tool_Fnc::writefile($tRunlogdir , $tTime);

			$this->opt();
			sleep(5);
		}
		exit;
	}
    public function opt(){
        $tCTCMO = new Huadao_Core_T_ClaimModel;                           
        $tCTC2MO = new Huadao_Core_T_ClaimcloseModel;
        $tHDMO = new HuadaoModel;
        $tUCMO = new User_CaseModel;
        $tCTIMO = new Huadao_Core_T_InsuredModel;
        $tUCLMO = new User_Case_LogModel;

        $tTime = time();

        $tUMO  = new UserModel;
        #$tCTCDatas = $tCTCMO->field('*')->where('status = 0 and sourceflag = 1')->fList();
        $tSql = 'select pay_remark,pdfnotice,insuredgrade,ctc.id id,notpayremark,ctc.caseno caseno,ctc.insurance_no insurance_no,policyno,requesttime,pay_type,registcasetime,pay_amnt,finishedcasetime,idtype,idno,loss_notice,name from (select * from ' . $tCTCMO->table . ' where status = 0 and sourceflag = 0) ctc , '.$tCTC2MO->table . ' ctc2 where ctc.caseno = ctc2.caseno';
        $tRes = $tCTCMO->query($tSql);
        foreach($tRes as $tRow){
            //删除已存在的线下案件
            $tSql = 'delete from ' . $tUCMO->table . ' where hd_caseno = \''.$tRow['caseno'].'\' and sourceflag = 1';
            $tUCMO->exec($tSql);

            $tURow = $tUMO->field('id uid')->where('crittype = ' . $tRow['idtype'] . ' and critcode = \''.$tRow['idno'].'\' and name = \''.$tRow['name'].'\'')->fRow();
            if(empty($tURow['uid'])){continue;}

            //案件状态转换
            $tIsDishonor = $tRow['pay_type'];

            //保单
            $tPolicynameInfo = $tHDMO->getPolicyInfo($tRow['policyno']); 
            if(!count($tPolicynameInfo)){continue;}

            //保险公司
            $tInsuranceInfo = $tHDMO->getInsurance($tRow['insurance_no']);
            if(!count($tInsuranceInfo)){continue;}

            //属组
            $tPolicygroupremakArr = $tHDMO->getInsuredgroup($tRow['insurance_no'],$tRow['policyno']);

            $tData = array(
                'created' => $tTime,
                'status' => 4, 
                'uid' => $tURow['uid'], 
                'policyno' => $tRow['policyno'], 
                'upload_time' => strtotime($tRow['requesttime']), 
                'firstimg_time' => strtotime($tRow['requesttime']), 
                'is_dishonor' => $tIsDishonor, 
                'closing_time' => strtotime($tRow['finishedcasetime']), 
                'payment_amount' => $tRow['pay_amnt'],
                'username' => $tRow['name'],
                'insurance_name' => $tInsuranceInfo['insurance_name'],
                'policyname' => $tPolicynameInfo['policyname'],
                'insuredgrade' => $tRow['insuredgrade'],
                'loss_notice' => $tRow['pdfnotice'],
                'hd_caseno' => $tRow['caseno'],
                'policygroupremak' => $tPolicygroupremakArr['policygroupremak'],
                'insuredgroup' => $tPolicygroupremakArr['insuredgroup'],
                'sourceflag' => 1,
                'dishonor_introduce' => $tRow['pay_remark'],
                'datatype' => '',
                'updated' => strtotime($tRow['finishedcasetime']),
            );
            $tID = $tUCMO->insert($tData);
			if(!empty($tID)){
				//发送短信提醒
				$tSMO = new SmsModel;
				$tUMO = new UserModel;
				$tSql = 'select u.phone from '.$tUCMO->table.' uc left join '.$tUMO->table.' u on uc.uid = u.id where uc.id = '.$tID;
				$tPhone = $tUCMO->query($tSql)[0]['phone'];
				$tCritcode = $tUCMO->query($tSql)[0]['critcode'];
				if(!empty($tPhone)){
					$tHCTGMO = new Huadao_Core_T_GrouppolicyinsuredModel;
					$tSTMO = new Sms_TemplateModel;
					$tPolicyno = $tHCTGMO->field('POLICYNO')->where('INSUREDIDNO = "'.$tCritcode.'"')->fRow()['POLICYNO'];
					$tMid = $tSTMO->field('tpl_id')->where('policyno = "'.$tPolicyno.'" and type = 4')->fRow()['tpl_id'];
					$tSMO->caseStatus($tPhone,$tMid);
				}
				
			}
			
            //log
            $tData = array(
                'title' => '申请赔付',
                'created' => strtotime($tRow['requesttime']), 
                'case_id' => $tID,
            ); 
            $tUCLMO->insert($tData);

            $tData = array(
                'id' => $tRow['id'], 
                'status' => 1,
            );
            $tCTCMO->update($tData);
        }
    }
}
